// 排行榜页面：支持音乐播放
const audio = wx.getBackgroundAudioManager()

Page({
  data: {
    current: {},
    playing: false,
    progress: 0,
    songs: [
      {
        id: 1,
        rank: 1,
        title: '打火机',
        artist: 'Penny',
        album: '打火机',
        isVip: true,
        isTrial: true,
        isHD: true,
        canPlay: true,
        url: 'https://www.kozco.com/tech/LRMonoPhase4.mp3'
      },
      {
        id: 2,
        rank: 2,
        title: 'Star Crossing Night (feat. GALI)',
        artist: '徐明浩/GALI',
        album: 'Star Crossing Ni...',
        change: 1,
        isVip: true,
        isTrial: true,
        isHD: true,
        canPlay: true,
        url: 'https://www.kozco.com/tech/piano2-CoolEdit.mp3'
      },
      {
        id: 3,
        rank: 3,
        title: '还是会想你',
        artist: '林达浪/h3R3',
        album: '还是会想你',
        change: 3,
        isHD: true,
        canPlay: true,
        url: 'https://www.kozco.com/tech/organfinale.mp3'
      },
      {
        id: 4,
        rank: 4,
        title: '罗生门(Follow)',
        artist: '张杰',
        album: '罗生门',
        change: -2,
        isVip: true,
        isTrial: true,
        canPlay: true
      },
      {
        id: 5,
        rank: 5,
        title: '特别的人(Special Person)',
        artist: '方大同',
        album: '特别的人',
        change: 1,
        isVip: true,
        isTrial: true,
        isHD: true,
        canPlay: true
      },
      {
        id: 6,
        rank: 6,
        title: 'The Fate of Ophelia',
        artist: 'Taylor Swift',
        album: 'The Life of a Sho...',
        change: -9,
        isVip: true,
        isTrial: true,
        isHD: true,
        canPlay: false
      },
      {
        id: 7,
        rank: 7,
        title: 'Always Online (联想Idea Pad S...)',
        artist: '林俊杰',
        album: 'JJ陆',
        change: 1,
        isVip: true,
        isTrial: true,
        isImmersive: true,
        canPlay: false
      },
      {
        id: 8,
        rank: 8,
        title: '夜曲',
        artist: '周杰伦',
        album: '十一月的萧邦',
        change: -1,
        isVip: true,
        isTrial: true,
        isHD: true,
        canPlay: true
      },
      {
        id: 9,
        rank: 9,
        title: '青花瓷',
        artist: '周杰伦',
        album: '我很忙',
        change: 2,
        isVip: true,
        isTrial: true,
        canPlay: true
      },
      {
        id: 10,
        rank: 10,
        title: '稻香',
        artist: '周杰伦',
        album: '魔杰座',
        change: -3,
        isVip: true,
        isTrial: true,
        isHD: true,
        canPlay: true
      }
    ]
  },
  
  onShow(){
    this.bindAudioEvents()
  },
  playSong(e) {
    const id = e.currentTarget.dataset.id
    const song = this.data.songs.find(s => s.id === id)
    if (!song || !song.canPlay) {
      wx.showToast({ title: '该歌曲暂不支持播放', icon: 'none' })
      return
    }
    
    if (this.data.current.id === id && this.data.playing) {
      audio.pause()
      this.setData({ playing: false })
      return
    }
    
    // 播放音乐
    audio.title = song.title
    audio.epname = '音乐排行榜'
    audio.singer = song.artist
    audio.coverImgUrl = song.cover || 'https://picsum.photos/seed/cover/200'
    audio.src = song.url
    
    this.setData({ current: song, playing: true })
    this.addHistory(song)
  },
  bindAudioEvents(){
    audio.onPlay(() => this.setData({ playing: true }))
    audio.onPause(() => this.setData({ playing: false }))
    audio.onStop(() => this.setData({ playing: false, progress: 0 }))
    audio.onEnded(() => this.next())
    audio.onTimeUpdate(() => {
      try { 
        this.setData({ progress: audio.currentTime / audio.duration }) 
      } catch(e) {}
    })
  },
  next(){
    const { songs, current } = this.data
    const idx = Math.max(0, songs.findIndex(s => s.id === current.id))
    const next = songs[(idx + 1) % songs.length]
    if (next && next.canPlay) {
      this.setData({ current: {} }, () => { this.playByObj(next) })
    }
  },
  playByObj(song){
    if (!song || !song.canPlay) return
    audio.title = song.title
    audio.singer = song.artist
    audio.coverImgUrl = song.cover || 'https://picsum.photos/seed/cover/200'
    audio.src = song.url
    this.setData({ current: song, playing: true })
    this.addHistory(song)
  },
  addHistory(song){
    const list = wx.getStorageSync('playHistory') || []
    const filtered = list.filter(i => i.id !== song.id)
    filtered.unshift({ 
      id: song.id, 
      name: song.title, 
      artist: song.artist, 
      cover: song.cover || 'https://picsum.photos/seed/cover/200', 
      ts: Date.now() 
    })
    wx.setStorageSync('playHistory', filtered.slice(0, 50))
  },
  togglePlay(){
    if (this.data.playing) {
      audio.pause()
    } else {
      audio.play()
    }
  },
  
  showMore(e) {
    const id = e.currentTarget.dataset.id
    const song = this.data.songs.find(s => s.id === id)
    if (song) {
      wx.showActionSheet({
        itemList: ['加入收藏', '分享', '下载', '查看详情'],
        success: (res) => {
          const actions = ['加入收藏', '分享', '下载', '查看详情']
          wx.showToast({
            title: `选择了: ${actions[res.tapIndex]}`,
            icon: 'none'
          })
        }
      })
    }
  },
  

})


